package com.it.mongo.service.impl;

import com.it.mongo.model.dto.BookDTO;
import com.it.mongo.service.BookMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.regex.Pattern;


@Service
public class BookMongoServiceImpl implements BookMongoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 新增
     * @param bookDTO
     * @return
     */
    @Override
    public Boolean inserBookInfo(BookDTO bookDTO) {
        mongoTemplate.insert(bookDTO);
        return true;
    }

    /**
     * 批量新增
     * @param bookDTOList
     * @return
     */
    @Override
    public Boolean inserBatchBookInfo(List<BookDTO> bookDTOList) {
        mongoTemplate.insert(bookDTOList,BookDTO.class);
        return  true;
    }

    /**
     * 修改
     * @param query
     * @param update
     * @return
     */
    @Override
    public Boolean updateBookInfo(Query query, Update update) {
        mongoTemplate.updateMulti(query,update,BookDTO.class);
        return true;
    }

    /**
     * 查所有
     * @return
     */
    @Override
    public  List<BookDTO> findAllBookInfo() {
        List<BookDTO> bookDTOList = mongoTemplate.findAll(BookDTO.class);
        return bookDTOList;
    }

    /**
     * 根据书名查询: 精准
     * @param name
     * @return
     */
    @Override
    public List<BookDTO> findBookInfoByName(String name) {
        Query query = Query.query(Criteria.where("name").is(name));
        return mongoTemplate.find(query,BookDTO.class);
    }

    /**
     * 根据书名查询: 模糊
     * @param name
     * @return
     */
    @Override
    public List<BookDTO> findBookInfoReName(String name) {
        // ^.*表示字符前面、*$表示字符后面
        Pattern compile = Pattern.compile("^.*" + name + ".*$", Pattern.CASE_INSENSITIVE);
        Query query= Query.query(Criteria.where("name").regex(compile));
        return mongoTemplate.find(query,BookDTO.class);
    }

    /**
     * 根据阅读量：范围查询并排序
     * @param minReadedNum
     * @param maxReadedNum
     * @return
     */
    @Override
    public List<BookDTO> findBookInfoReRange(Double minReadedNum, Double maxReadedNum) {
        // query两种方式
        Query query = Query.query(Criteria.where("readedNum").gte(minReadedNum).lte(maxReadedNum))
                .with(Sort.by(Sort.Direction.ASC,"readedNum"));
        return  mongoTemplate.find(query,BookDTO.class);
    }

    /**
     * 排序-分页查询
     * @param minReadedNum
     * @param maxReadedNum
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<BookDTO> findBookInfoPage(Double minReadedNum, Double maxReadedNum, Integer pageNum, Integer pageSize) {
        Query query = Query.query(Criteria.where("readedNum").gte(minReadedNum).lte(maxReadedNum))
                .with(Sort.by(Sort.Direction.ASC,"readedNum"))
                .with(PageRequest.of(pageNum-1,pageSize));
        return mongoTemplate.find(query, BookDTO.class);
    }

}
